package com.shungen.realapigateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shungen.realapigateway.mapper.UserMapper;
import com.shungen.realapigateway.model.entity.User;
import com.shungen.realapigateway.service.AuthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shungen
 * @date 2024年08月10日 12:06
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private UserMapper userMapper;

    /**
     * 根据访问秘钥获取访问secretKey
     * @param accessKey
     * @return
     */
    @Override
    public String getSecretKeyByAccessKey(String accessKey) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccessKey,accessKey);
        User user = userMapper.selectOne(queryWrapper);
        return user.getSecretKey();
    }


    /**
     * 根据1访问对象获取用户对象信息
     * @param accessKey
     * @return
     */
    @Override
    public User getUserByAccessKey(String accessKey) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccessKey,accessKey);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }


    /**
     * 扣除积分
     * @param id
     * @param price
     * @return
     */
    @Override
    public boolean subUserPoint(Long id, Long price) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.setSql("pointBalance = pointBalance - 3").eq(User::getId,id);
        int update = userMapper.update(null,updateWrapper);
        return update == 1;
    }
}
